package com.bole.controller;

import com.bole.data.Result;
import com.bole.data.ResultEnum;
import com.bole.pojo.TbMenuDo;
import com.bole.service.MenuService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("menu")
@Api(value = "目录接口api")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @GetMapping
    public Result getMenu(Long id) {
        List<Map<String, Object>> menu = menuService.getMenu(id);
        return Result.response(ResultEnum.OK).setData(menu);
    }

    @GetMapping("/zTree")
    public Result getzTree() {
        List<Map<String, Object>> maps = menuService.getzTree();
        return Result.response(ResultEnum.OK).setData(maps);
    }

    @PostMapping
    public Result add(@RequestBody TbMenuDo menu) {
        menuService.addMenu(menu);
        return Result.response(ResultEnum.OK);
    }

    @GetMapping("/{id}")
    public Result getMenuById(@PathVariable("id") Long id) {
        TbMenuDo menu = menuService.getMenuById(id);
        return Result.response(ResultEnum.OK).setData(menu);
    }

    @PutMapping("/{id}")
    public Result updateById(@PathVariable("id") Long id, @RequestBody TbMenuDo menu) {
        menu.setId(id);
        menuService.updateById(menu);
        return Result.response(ResultEnum.OK);
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Long id) {
        menuService.deleteById(id);
        return Result.response(ResultEnum.OK);
    }

    @GetMapping("/getzTreeChecked/{rid}")
    public Result getzTreeChecked(@PathVariable("rid") Long rid) {
        List<Map<String, Object>> maps = menuService.getzTreeChecked(rid);
        return Result.response(ResultEnum.OK).setData(maps);
    }

}
